package com.ch.ebusiness.config;

import com.ch.ebusiness.entity.BUser;
import com.ch.ebusiness.repository.before.UserRepository;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Author MarsYr
 * @Date 2021/7/14 15:56
 * @Version 1.0
 */

@Component
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
    @Resource
    private UserRepository userRepository;
    
    @Resource
    private BCryptPasswordEncoder bCryptPasswordEncoder;
    
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        BUser user = userRepository.findUser(s);
        if (user == null) {
            throw new UsernameNotFoundException("用户不存在！");
        }
        String password = bCryptPasswordEncoder.encode("123456");
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        list.add(new SimpleGrantedAuthority("ADMIN"));
        return new User(s, password, list);
    }
}

